package com.itheima.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.exception.NotAllowedToDeleteClazzException;
import com.itheima.mapper.ClazzMapper;
import com.itheima.mapper.StudentMapper;
import com.itheima.pojo.Clazz;
import com.itheima.pojo.ClazzQueryParam;
import com.itheima.pojo.ClazzResult;
import com.itheima.pojo.Student;
import com.itheima.service.ClazzService;
import com.itheima.service.EmpService;
import com.itheima.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class ClazzServiceImpl extends ServiceImpl<ClazzMapper, Clazz> implements ClazzService {
    @Autowired
    private ClazzMapper clazzMapper;

    @Autowired
    private StudentMapper studentMapper;

    @Autowired
    private StudentService studentService;

    @Autowired
    private EmpService empService;

    /*mybatis-plus*/
    //列表查询
    @Override
    public ClazzResult pageClazz(ClazzQueryParam queryParam) {
        //添加条件
        LambdaQueryWrapper<Clazz> queryWrapper = new LambdaQueryWrapper<Clazz>();
        queryWrapper.like(queryParam.getName()!=null && !queryParam.getName().isEmpty(),Clazz::getName,queryParam.getName())
                .between(queryParam.getBegin()!=null && queryParam.getEnd()!=null,Clazz::getBeginDate,queryParam.getBegin(),queryParam.getEnd());

        //获取分页数据
        Page<Clazz> page = Page.of(queryParam.getPage(),queryParam.getPageSize());
        //查询
        page = this.page(page,queryWrapper);

        //封装Clazz
        //1.查询clazz
        List<Clazz> records = page.getRecords();

        //2.查询员工
        for (Clazz clazz : records) {
            //封装班主任名字
            Integer masterId = clazz.getMasterId();
            String masterName = empService.getById(masterId).getName();
            clazz.setMasterName(masterName);
            //封装班级状态
            //计算当前日期范围
            if(LocalDate.now().isBefore(clazz.getBeginDate())){
                clazz.setStatus("未开班");
            }else if(LocalDate.now().isAfter(clazz.getEndDate())){
                clazz.setStatus("已结课");
            }else
                clazz.setStatus("已开班");
        }
        return new ClazzResult(page.getTotal(), records);
    }

    //删除班级
    @Override
    public void deleteClazz(Integer id) {
        //判断该班级下是否还有学生，如果有则不允许删除
        List<Student> list = studentService.list(new LambdaQueryWrapper<Student>().eq(Student::getClazzId, id));
        if(!list.isEmpty()){
            throw new NotAllowedToDeleteClazzException("对不起, 该班级下有学生, 不能直接删除!");
        }

        //没有学生，直接删除
        this.removeById(id);
    }







/*    //3.1 班级列表查询
    @Override
    public ClazzResult getPage(ClazzQueryParam queryParam) {
        return null;
    }

    //3.2 删除班级
    @Override
    public void deleteById(Integer id) {
        //判断该班级下是否还有学生，如果有则不允许删除
        //1. 查询班级下的学生数量,根据班级id查询学生数量，返回集合
        Integer count = studentMapper.getCountByClazzId(id);
        if(count!=0){
            throw new NotAllowedToDeleteClazzException("对不起, 该班级下有学生, 不能直接删除!");
        }
        //如果没有学生则删除班级
        clazzMapper.deleteById(id);
    }

    //3.3 添加班级
    @Override
    public void insertClazz(Clazz clazz) {
        clazz.setCreateTime(LocalDateTime.now());
        clazz.setUpdateTime(LocalDateTime.now());
//        System.out.println("DEBUG - clazz.createTime: " + clazz.getCreateTime());
        clazzMapper.insertMapperClazz(clazz);
    }

    //3.4根据id查询
    @Override
    public Clazz getById(Integer id) {
        return clazzMapper.getById(id);
    }

    //3.5修改班级
    @Override
    public void updateById(Clazz clazz) {
        clazzMapper.updateById(clazz);
    }

    //3.6查询所有班级
    @Override
    public List<Clazz> selectAllClazz() {
        return clazzMapper.selectMapperAllClazz();
    }*/
}
